package com.ymkj.testdemo.annotation;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Component
public class OperateAspect {

         @Pointcut("@annotation(com.ymkj.testdemo.annotation.Test)")
         public void annotationPointCut() {

         }
//       ​
        @Before("annotationPointCut()")
        public void before(JoinPoint joinPoint){
           MethodSignature sign =  (MethodSignature)joinPoint.getSignature();
           Method method = sign.getMethod();
           Test annotation = method.getAnnotation(Test.class);
           System.out.print("打印："+annotation.value()+" 开始前");
        }    
        @Around("annotationPointCut()")
        public Object advice(ProceedingJoinPoint joinPoint){
            System.out.println("通知之开始");
            Object retmsg=null;
            try {
                 retmsg=joinPoint.proceed();
                System.err.println("++++++++"+retmsg);
            } catch (Throwable e) {
                e.printStackTrace();
            }
            System.out.println("通知之结束");
            return retmsg;
        }

        @After("annotationPointCut()")
        public void after() {
             System.out.println("after方法执行后");
        }

}
